package com.sanhe.a448.utils;

import static com.menu.app.util.port.SerialHandle.byte2int;

import android.content.Intent;
import android.os.Looper;
import android.util.Log;

import com.menu.app.MyApp;
import com.menu.app.model.Models;
import com.menu.app.util.port.SerialHandler;
import com.plata.base.utils.WeakHandler;
import com.sanhe.a448.activity.work.ModeActivity;
import com.sanhe.a448.activity.work.TimeActivity;
import com.sanhe.a448.activity.work.WorkActivity;
import com.sanhe.a448.event.Event;

public class MySerialHandler implements SerialHandler {

    WeakHandler h = new WeakHandler(Looper.getMainLooper());

    @Override
    public int dealMsg(Byte[] data, boolean ignore) {
        if (!Models.Companion.get().getStartEvent().getValue()) {
            Models.Companion.get().getStartEvent().postValue(true);
        }
        if (data[2] == 7) {
            int i = byte2int(data[9]);
            h.post(() -> gotoPage(i, ignore));
        } else if (data[3] == (byte) 0x82) {
            if (data[4] == (byte) 0x1f && data[5] == (byte) 0x15) {
                Event.mode.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xb7) {
                Event.stateOne.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xb8) {
                Event.stateTwo.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xb6) {
                Event.stateFour.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xb1) {
                Event.stateSurface.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0xb2) {
                Event.stateDeep.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && (data[5] >= (byte) 0x50 && data[5] <= (byte) 0x54)) {
                Event.bigEnergyEvent.setValue(0x54 - data[5], data[7]);
            } else if (data[4] == (byte) 0x1f && (data[5] >= (byte) 0x60 && data[5] <= (byte) 0x64)) {
                Event.smallEnergyEvent.setValue(0x64 - data[5], data[7]);
            } else if (data[4] == (byte) 0x1f && (data[5] >= (byte) 0x10 && data[5] <= (byte) 0x11)) {
                Event.time.setValue(data[5] - 0x10, data[7]);
            } else if (data[4] == (byte) 0x1f && (data[5] >= (byte) 0x17 && data[5] <= (byte) 0x18)) {
                Event.power.setValue(data[5] - 0x17, data[7]);
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x40) {
                Event.timeProgress.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x41) {
                Event.powerProgress.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x2f) {
                Event.alert.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x1b) {
                Event.start.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x1e) {
                Event.msg.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x1f && data[5] == (byte) 0x1f) {
                Event.tip.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x25 && data[5] <= (byte) 0x28)) {
                Event.day.setValue(0x28 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x23 && data[5] <= (byte) 0x24)) {
                Event.hour.setValue(0x24 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x21 && data[5] <= (byte) 0x22)) {
                Event.min.setValue(0x22 - data[5], data[7]);
            } else if (data[4] == (byte) 0x1f && (data[5] >= 0 && data[5] <= 5)) {
                Event.version.setValue(data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x45 && data[5] <= (byte) 0x48)) {
                Event.cetDay.setValue(0x48 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x43 && data[5] <= (byte) 0x44)) {
                Event.cetHour.setValue(0x44 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x41 && data[5] <= (byte) 0x42)) {
                Event.cetMin.setValue(0x42 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x35 && data[5] <= (byte) 0x38)) {
                Event.retDay.setValue(0x38 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x33 && data[5] <= (byte) 0x34)) {
                Event.retHour.setValue(0x34 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x31 && data[5] <= (byte) 0x32)) {
                Event.retMin.setValue(0x32 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x55 && data[5] <= (byte) 0x58)) {
                Event.hspDay.setValue(0x58 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x53 && data[5] <= (byte) 0x54)) {
                Event.hspHour.setValue(0x54 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && (data[5] >= (byte) 0x51 && data[5] <= (byte) 0x52)) {
                Event.hspMin.setValue(0x52 - data[5], data[7]);
            } else if (data[4] == (byte) 0x11 && data[5] == (byte) 0x62) {
                Event.cetClean.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x11 && data[5] == (byte) 0x61) {
                Event.retClean.postValue(byte2int(data[7]));
            } else if (data[4] == (byte) 0x11 && data[5] == (byte) 0x63) {
                Event.hspClean.postValue(byte2int(data[7]));
            }
        }
        return 0;
    }

    private void gotoPage(int index, boolean ignore) {
        Log.e("taggg", "goto page " + index);
        Const.currentPage = index;
        Class c = null;
        Intent i = new Intent();
        if (index <= 1) {
            c = ModeActivity.class;
        } else if (index <= 3 || index == 6 || index == 7) {
            c = WorkActivity.class;
        } else if (index == 4) {
            Const.sendTouchEvent(0x83, 0x10, 0xc, 1, 0x1d);
            return;
//            c = SetActivity.class;
        } else if (index == 8) {
            c = TimeActivity.class;
        }
        if (c == null || ignore) {
            return;
        }
        i.setClass(MyApp.getApp(), c);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.getApp().startActivity(i);
    }
}
